﻿using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Collections.Specialized;
using System.Web;
public enum FilterFieldType : int
{
    All = 1,
    OnlyPost = 2,
    OnlyGet = 3
}
namespace WebTeamplate
{
    public class FilterFields : NameObjectCollectionBase
    {

        private string FilterName(string s)
        {
            if (s == string.Empty)
                return string.Empty;

            string[] aArray = s.Split('$');
            return aArray[aArray.Length - 1];
        }
        protected void AddUnique(string name, string value)
        {
            if (name.StartsWith("F_") | name.Contains("$F_"))
            {
                object data = BaseGet(name);
                if (data == null)
                {
                    // if (!(value == string.Empty))
                    BaseAdd(FilterName(name), value);
                }
            }
        }
        public void Add(string name, string value)
        {
            if (name.StartsWith("F_") | name.Contains("$F_"))
            {
                object data = BaseGet(name);
                if (data == null)
                {
                    // if (!(value == string.Empty))
                    BaseAdd(FilterName(name), value);
                }
            }
        }
        public void Add(string name, NameValueCollection values, string keyName)
        {
            if ((values[keyName] != null))
            {
                foreach (string val in values.GetValues(keyName))
                {
                    if (name.StartsWith("F_") | name.Contains("$F_"))
                    {
                        if (!(val == string.Empty))
                            BaseAdd(FilterName(name), System.Web.HttpUtility.UrlEncode(val));
                    }
                }
            }
        }
        public string ToString()
        {
            return ToString("");
        }
        public string ToString(string removeList)
        {
            string Params = string.Empty;
            string[] list = new string[2];

            if (!(removeList == string.Empty))
            {
                list = removeList.Split(';');
            }

            for (int i = 0; i <= Count - 1; i++)
            {
                if (Array.IndexOf(list, BaseGetKey(i)) < 0)
                {
                    if (!(BaseGet(i) == string.Empty))
                        Params += FilterName(BaseGetKey(i)) + "=" + Convert.ToString(BaseGet(i)) + "&";
                }
            }
            Params = Params.TrimEnd('&');
            return Params;
        }
        public string ToString(FilterFieldType preserve, string removeList, System.Web.UI.StateBag viewState)
        {
            HttpRequest Request = HttpContext.Current.Request;
            HttpServerUtility Server = HttpContext.Current.Server;
            string[] list = new string[2];
            if (!(removeList == string.Empty))
            {
                list = removeList.Split(';');
            }
            if (preserve == FilterFieldType.All | preserve == FilterFieldType.OnlyPost)
            {
                for (int i = 0; i <= Request.Form.Count - 1; i++)
                {
                    if (Array.IndexOf(list, Request.Form.AllKeys[i]) < 0 && Request.Form.AllKeys[i] != "__EVENTTARGET" && Request.Form.AllKeys[i] != "__EVENTARGUMENT" && Request.Form.AllKeys[i] != "__VIEWSTATE" && BaseGet(Request.Form.AllKeys[i]) == null)
                    {
                        foreach (string val in Request.Form.GetValues(i))
                        {
                            //  if (!(val == string.Empty))
                            AddUnique(FilterName(Request.Form.AllKeys[i]), Server.UrlEncode(val));
                        }
                    }
                }
            }
            if (preserve == FilterFieldType.All | preserve == FilterFieldType.OnlyGet)
            {
                if ((viewState != null))
                {
                    int length = viewState.Count;
                    string[] keys = new string[length + 1];
                    System.Web.UI.StateItem[] values = new System.Web.UI.StateItem[length + 1];

                    viewState.Keys.CopyTo(keys, 0);
                    viewState.Values.CopyTo(values, 0);

                    for (int i = 0; i <= length - 1; i++)
                    {
                        if ((values[i].Value != null))
                        {
                            //  if (!(values[i].Value.ToString() == string.Empty))
                            AddUnique(FilterName(keys[i]), Server.UrlEncode(values[i].Value.ToString()));
                        }
                    }
                }
                for (int i = 0; i <= Request.QueryString.Count - 1; i++)
                {
                    if (Array.IndexOf(list, Request.QueryString.AllKeys[i]) < 0 && BaseGet(Request.QueryString.AllKeys[i]) == null)
                    {
                        foreach (string val in Request.QueryString.GetValues(i))
                        {
                            // if (!(val == string.Empty))
                            AddUnique(FilterName(Request.QueryString.AllKeys[i]), Server.UrlEncode(val));
                        }
                    }
                }
            }
            return ToString("");
        }
    }
}
